import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { AdminRoleSchema } from 'src/endpoints/admin/admin-roles/entities/admin-role.entity';
import { ConfigModule } from 'src/config/config.module';
import { TypedConfigService } from 'src/config/typed-config.service';

@Module({
    imports: [
        ConfigModule, // 确保ConfigModule被导入
        MongooseModule.forRootAsync({
            imports: [ConfigModule],
            useFactory: async (configService: TypedConfigService) => {
                const dbConfig = configService.get('mongodb'); // 现在有完整的类型提示
				let connectionString =`mongodb://${dbConfig.username}:${dbConfig.password}@${dbConfig.host}:${dbConfig.port}/${dbConfig.database}?authSource=${dbConfig.authSource}`
				if(dbConfig.replicaSet){
					connectionString = `${connectionString}&replicaSet=${dbConfig.replicaSet}`
				}
				return {
                    uri: connectionString
                };
            },
            inject: [TypedConfigService],
        }),
        MongooseModule.forFeature([AdminRoleSchema]),
    ],
})
export class MongodbModule { }
